
#-----------------------------------


ActiveRecord::Base.default_timezone = :utc

class InterleagueOrganization < ActiveRecord::Base
  belongs_to :location
  has_many   :interleague_memberships
  has_many   :leagues, :through => :interleague_memberships
  has_many   :interleague_organization_staff_positions
  has_many   :staffers, :through => :interleague_organization_staff_positions
  has_one    :note
end

class InterleagueMembership < ActiveRecord::Base
  belongs_to :interleague_organization
  belongs_to :league
  has_one    :note
end

class League < ActiveRecord::Base
  belongs_to :location
  has_many   :interleague_memberships
  has_many   :interleague_organizations, :through => :interleague_memberships
  has_many   :teams
  has_many   :league_staff_positions
  has_many   :staffers, :through => :league_staff_positions
  has_one    :note
end

class Team < ActiveRecord::Base
  belongs_to :league
  has_many   :skater_teams
  has_many   :skaters, :through => :skater_teams
  has_many   :team_staff_positions
  has_many   :staffers, :through => :team_staff_positions
  has_many   :game_teams
  has_many   :games, :through => :game_teams
  has_one    :note
end

#-----------------------------------

class Skater < ActiveRecord::Base
  belongs_to :person
  has_many   :skater_teams
  has_many   :teams, :through => :skater_teams
  has_many   :game_skaters
  has_many   :games, :through => :game_skaters
  has_one    :note
end

class SkaterTeam < ActiveRecord::Base
  belongs_to :skater
  belongs_to :team
  has_many   :team_captains 
  has_one    :note
end

class TeamCaptain < ActiveRecord::Base
  belongs_to :skater_team
  has_one    :note
end

class Staffer < ActiveRecord::Base
  belongs_to :person
  has_many   :interleague_organization_staff_positions
  has_many   :interleague_organizations, :through => :interleague_organization_staff_positions
  has_many   :league_staff_positions
  has_many   :leagues, :through => :league_staff_positions
  has_many   :team_staff_positions
  has_many   :teams, :through => :team_staff_positions
  has_one    :note
end

class InterleagueOrganizationStaffPosition < ActiveRecord::Base
  belongs_to :interleague_organization
  belongs_to :staffer
  has_one    :note
end

class LeagueStaffPosition  < ActiveRecord::Base
  belongs_to :league
  belongs_to :staffer
  has_one    :note
end

class TeamStaffPosition  < ActiveRecord::Base
  belongs_to :team
  belongs_to :staffer
  has_one    :note
end

#-----------------------------------

class Bout < ActiveRecord::Base
  has_many   :games
  has_one    :note
end

class Game < ActiveRecord::Base
  belongs_to :bout
  acts_as_list :column => :game_number, :scope => :bout_id
  has_many   :game_teams, :order => :position
  has_many   :teams,   :through => :game_teams
  has_many   :game_skaters
  has_many   :skaters, :through => :game_skaters
  has_many   :periods, :order => :period_number
  has_one    :note

  def score()
    game_score = {
      :home    => 0,
      :visitor => 0
    }
    periods(true).each { |p|
      ps = p.score()
      game_score[:home]    += ps[:home]
      game_score[:visitor] += ps[:visitor]
    }
    return game_score
  end

end

class Period < ActiveRecord::Base
  belongs_to :game
  acts_as_list :column => :period_number, :scope => :game_id
  has_many   :jams, :order => :jam_number
  has_one    :note

  def score()
    period_score = {
      :home    => 0,
      :visitor => 0
    }
    jams(true).each { |j|
      js = j.score()
      period_score[:home]    += js[:home]
      period_score[:visitor] += js[:visitor]
    }
    return period_score
  end
end

class Jam < ActiveRecord::Base
  belongs_to :period
  acts_as_list :column => :jam_number, :scope => :period_id
  has_many   :skater_jams
  has_many   :game_skaters, :through => :skater_jams
  has_many   :passes, :order => :pass_number  # jammer or pivot(pass-the-star) only
  has_one    :note

  @@POSITIONS = [:jammer, :pivot, :blocker, :blocker, :blocker]
  @@ORDER_BY = {
    :jammer  => 0,
    :pivot   => 1,
    :blocker => 2
  }

  
  attr_reader :home_skaters
  attr_reader :visitor_skaters

  def home_skaters
    
  end

  def _initialize()
    @home_skaters    = @@POSITIONS.map { |p| SkaterJam.create(:position => p) }
    @visitor_skaters = @@POSITIONS.map { |p| SkaterJam.create(:position => p) }

    unless new_record?
    end
    
  end

  def set_positions(gslist)
    h = skater_jams(true).find_all{ |sj| sj.game_skater.is_home_team == 'Y' }
    v = skater_jams.find_all{ |sj| sj.game_skater.is_home_team == 'N' }
    gslist.each_index { |j|
      gs = gslist[j];
      p = @@POSITION[j];
      if skater_jams[j].nil?
        skater_jams[j] = SkaterJam.create(:game_skater => gs, :position => p)
      else
        skater_jams[j].game_skater = gslist[j]
      end
    }
  end



  def score()
    jam_score = {
      :home    => 0,
      :visitor => 0
    }
    ps = passes(true)
    return jam_score if ps.nil? || ps.empty?
    ps.each { |p|
      side = p.skater_jam.game_skater.is_home_team == 'Y' ? :home : :visitor
      jam_score[side] += p.points
    }
    return jam_score
  end
end

class Pass < ActiveRecord::Base
  belongs_to :skater_jam
  belongs_to :jam
  acts_as_list :column => :pass_number, :scope => :jam_id
  has_one    :note

end

#-----------------------------------

#-----------------------------------

class GameTeam < ActiveRecord::Base
  belongs_to :game
  acts_as_list :scope => :game_id
  belongs_to :team
  has_one    :note
end

class GameSkater < ActiveRecord::Base
  belongs_to :game
  belongs_to :skater
  has_many   :skater_jams
  has_many   :jams, :through => :skater_jams
  has_many   :skater_penalty_box_sessions, :order => :session_number
  has_one    :note

  def minors()
    skater_jam_minors = Array.new
    skater_jams(true).each { |sj|
      skater_jam_minors << sj.skater_jam_penalties(true).find_all {|sjp| sjp.is_major == 'N'}
    }
    return skater_jam_minors
  end

  def majors()
    skater_jam_majors = Array.new
    skater_jams(true).each { |sj|
      skater_jam_majors << sj.skater_jam_penalties(true).find_all {|sjp| sjp.is_major == 'Y'}
    }
    return skater_jam_majors
  end

  def majors_for_period(period_number)
    return majors().find_all {|p| p.skater_jam.jam.period.period_number == period_number}
  end
end

class SkaterJam < ActiveRecord::Base
  belongs_to :jam
  belongs_to :game_skater
  has_many   :skater_jam_penalties, :order => :penalty_number
  has_many   :passes  # jammer or pivot(pass-the-star) only
  has_one    :note
end

#-----------------------------------

class Penalty < ActiveRecord::Base
  has_one    :note
end

class SkaterJamPenalty < ActiveRecord::Base
  belongs_to :skater_jam
  acts_as_list :column => :penalty_number, :scope => :skater_jam_id
  belongs_to :penalty
  has_one    :note
end

class SkaterPenaltyBoxSession < ActiveRecord::Base
  belongs_to :game_skater
  acts_as_list :column => :session_number, :scope => :game_skater_id
  belongs_to :penalty
  has_one    :note
end

#-----------------------------------

class Location < ActiveRecord::Base
  belongs_to :city
  has_many   :interleague_organizations
  has_many   :leagues
  has_one    :note
end

class City < ActiveRecord::Base
  belongs_to :state
  has_many   :locations
  has_one    :note
end

class State < ActiveRecord::Base
  belongs_to :country
  has_many   :cities
  has_one    :note
end

class Country < ActiveRecord::Base
  has_many   :states
  has_one    :note
end

#-----------------------------------

class Note < ActiveRecord::Base
    belongs_to :interleague_organizations
    belongs_to :interleague_memberships
    belongs_to :leagues
    belongs_to :teams
#-----------------------------------
    belongs_to :skaters
    belongs_to :skater_teams
    belongs_to :staffers
    belongs_to :interleague_organization_staff_positions
    belongs_to :league_staff_positions
    belongs_to :team_staff_positions
#-----------------------------------
    belongs_to :bouts
    belongs_to :games
    belongs_to :periods
    belongs_to :jams
    belongs_to :passes
    belongs_to :game_teams
    belongs_to :game_skaters
    belongs_to :skater_jams
#-----------------------------------
    belongs_to :penalties
    belongs_to :skater_jam_penalties
    belongs_to :skater_penalty_box_sessions 
#-----------------------------------
    belongs_to :locations
    belongs_to :cities
    belongs_to :states
    belongs_to :countries
end


#-----------------------------------
#-----------------------------------
#-----------------------------------

class People < ActiveRecord::Base
  has_many   :skaters
  has_many   :staffers

  establish_connection(
    :adapter  => "mysql",
    :database => "people_development",
    :username => "derbyadmin",
    :password => "fakepassword",
    :host     => "localhost"
  )
end

#-----------------------------------
#-----------------------------------
#-----------------------------------


